﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ProjectTest.Models
{
    public class QLHocPhan
    {
        private DBTestDataContext db = new DBTestDataContext();


        //danh sach sinh vien
        public IEnumerable<HocPhan> GetAll()
        {
            return db.HocPhans;
        }
       
        //Create
        public void Create(HocPhan hp)
        {
            try
            {
                db.HocPhans.InsertOnSubmit(hp);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
        //Edit
        public void Edit(HocPhan hpEdited, string mhp)
        {
            HocPhan hp = db.HocPhans.SingleOrDefault(p => p.MaHP == mhp);
            try
            {
                UpdateInfo(hp, hpEdited);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void UpdateInfo(HocPhan hp, HocPhan hpEdited)
        {
            hp.TenHP = hpEdited.TenHP;
            hp.TinChi = hpEdited.TinChi;
            hp.LoaiHP = hpEdited.LoaiHP;
            hp.HocKy = hpEdited.HocKy;
            hp.NamHoc = hpEdited.NamHoc;

        }
        //Delete
        public void Delete(string mhp)
        {
            HocPhan hp = db.HocPhans.SingleOrDefault(p => p.MaHP == mhp);
            try
            {
                db.HocPhans.DeleteOnSubmit(hp);
                db.SubmitChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
        //tim hoc phan theo ten hoc phan
        public IEnumerable<HocPhan> FindByName(string name)
        {
            return db.HocPhans.Where(p => p.TenHP.ToLower().Contains(name.ToLower()));
        }
    }
}